import 'package:huxiaodui/api/api.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:permission_handler/permission_handler.dart';

class QrScanPage extends StatefulWidget {
  const QrScanPage({super.key});
  @override
  State<QrScanPage> createState() => _QrScanPageState();
}

class _QrScanPageState extends State<QrScanPage>
    with SingleTickerProviderStateMixin {

  final MobileScannerController controller = MobileScannerController(
    torchEnabled: false,
    formats: [BarcodeFormat.qrCode],
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Text('扫一扫'),
            const SizedBox(width: 50),
            IconButton(
              color: Colors.white,
              icon: ValueListenableBuilder<TorchState>(
                valueListenable: controller.torchState,
                builder: (context, state, child) {
                  switch (state) {
                    case TorchState.off:
                      return const Icon(
                        Icons.flash_off,
                        color: Colors.white,
                      );
                    case TorchState.on:
                      return const Icon(
                        Icons.flash_on,
                        color: Colors.yellow,
                      );
                  }
                },
              ),
              iconSize: 32.0,
              onPressed: () {
                controller.toggleTorch();
              },
            ),
            IconButton(
              color: Colors.white,
              icon: const Icon(Icons.image),
              iconSize: 32.0,
              onPressed: () async {

                await Permission.camera.request();
                final ImagePicker picker = ImagePicker();
                final XFile? image = await picker.pickImage(
                  source: ImageSource.gallery,
                  imageQuality:100,
                );
                if (image != null) {
                  await controller.analyzeImage(image.path);
                }
              },
            ),
          ],
        ),
      ),
      body: Builder(
        builder: (context) {
          return Stack(
            children: [
              Positioned(
                child: MobileScanner(
                  controller: controller,
                  onDetect: (barcodeCapture) async {
                    String? code = (barcodeCapture.barcodes.map((e) => e.rawValue)).first;
                    if(code != null) {
                      controller.stop();
                      if(!await Api.unit.findByCode(code,isPushReplacement: true)){
                        controller.start();
                      }
                    }
                  },
                ),
              ),
              Positioned(
                child: Center(
                  child: Container(
                    width: 240,
                    height: 240,
                    child: Stack(
                      children: [
                        Container(
                          decoration: BoxDecoration(
                            border: Border.all(
                              color: Colors.deepOrange,
                              width: 2.0,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.all(10.0),
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            border: Border.all(
                              color: Colors.transparent,
                              width: 2.0,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  // @override
  // void dispose() {
  //   controller.dispose();
  //   super.dispose();
  // }
}
